package sz.szsmk.citizencard.ui.dzsmkavtivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.GetCodeResponse;
import sz.szsmk.citizencard.model.network.dzbean.res.BankCard;
import sz.szsmk.citizencard.model.network.dzbean.res.GetMyBankResponse;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.MyBankCardMvpView;
import sz.szsmk.citizencard.mvp.RechargeMvpView;
import sz.szsmk.citizencard.mvp.dzsmkpresenter.MyBankCardPresenter;
import sz.szsmk.citizencard.mvp.dzsmkpresenter.RechargePresenter;
import sz.szsmk.citizencard.util.PubUtils;
import sz.szsmk.citizencard.widget.PayPsdInputView;

/**
 * Created by chensf on 17/9/30.
 */

public class MyBankCardActivity extends AppBaseActivity implements MyBankCardMvpView, RechargeMvpView {

    RecyclerView cv_qr_my_bank_card;
    Button tv_qr_change_bank_card;

    @Inject
    MyBankCardPresenter mMyBankCardPresenter;
    @Inject
    RechargePresenter rechargePresenter;
    private BaseQuickAdapter mAdapter;
    private PopupWindow popupWindow;

    @Override
    public void onResume() {
        super.onResume();
//        mMyBankCardPresenter.gainMyBankCard();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(MyBankCardActivity.this);
        setContentView(R.layout.qr_activity_my_bank_card);

        cv_qr_my_bank_card = findViewById(R.id.cv_qr_my_bank_card);
        tv_qr_change_bank_card = findViewById(R.id.tv_qr_change_bank_card);
        tv_qr_change_bank_card.setOnClickListener(this::onClickView);

        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0f)
                .statusBarColor(R.color.white).init();
        cv_qr_my_bank_card.setLayoutManager(new LinearLayoutManager(MyBankCardActivity.this));
        mAdapter = new BaseQuickAdapter<BankCard, BaseViewHolder>(R.layout.qr_item_my_bank_card, new ArrayList<BankCard>()) {

            @Override
            protected void convert(BaseViewHolder helper, BankCard item) {
                helper.setText(R.id.tv_qr_bank_name, item.getBankName());
                helper.setText(R.id.tv_qr_last_card_number, item.getBankCardNumber());

                ImageView imageView = helper.getView(R.id.iv_qr_bank_url);
                Glide.with(MyBankCardActivity.this).load(item.getBankIconUrl()).placeholder(R.drawable.qr_ic_banklog).error(R.drawable.qr_ic_banklog).into(imageView);
            }
        };
        //好像不能点击...
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });

        cv_qr_my_bank_card.setAdapter(mAdapter);
        mMyBankCardPresenter.gainMyBankCard();
    }

    @Override
    protected CharSequence getTopTitle() {
        return getString(R.string.qr_my_bank_card);
    }

    @Override
    protected BasePresenter[] initPresenters() {
        return new BasePresenter[]{mMyBankCardPresenter, rechargePresenter};
    }


    public void onClickView(View v) {
        if (R.id.tv_qr_change_bank_card == v.getId()) {
            //TODO 到绑定银行卡流程一样去
            showPassword(v);
        }
    }

    @SuppressLint("WrongConstant")
    private void showPassword(View view) {
        View popView = getLayoutInflater().inflate(
                R.layout.qr_pop_password, null);
        final PayPsdInputView payPsdInputView = (PayPsdInputView) popView.findViewById(R.id.password);
        TextView tv_qr_cancel = (TextView) popView.findViewById(R.id.tv_qr_cancel);
        TextView tv_qr_ok = (TextView) popView.findViewById(R.id.tv_qr_ok);
        popupWindow = new PopupWindow(popView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);//popView即popupWindow的布局，ture设置focusAble.
        popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //必须设置BackgroundDrawable后setOutsideTouchable(true)才会有效。这里在XML中定义背景，所以这里设置为null;
//        popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
//        popupWindow.setOutsideTouchable(true); //点击外部关闭。
        popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);    //设置一个动画。
        backgroundAlpha(0.5f);
        //设置Gravity，让它显示在右上角。

        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);


        tv_qr_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        tv_qr_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = payPsdInputView.getPasswordString();
                if (password.length() < 6) {
                    showTipDialog("密码位数不全");
                } else {
                    popupWindow.dismiss();
                    showLoadingDialog();
                    rechargePresenter.checkPayPassword(password, password);
                }
            }
        });

        popupWindow.setOnDismissListener(new poponDismissListener());
    }

    private void showTipDialog(String tip) {
        PubUtils.popTipOrWarn(MyBankCardActivity.this, tip);
    }

    @Override
    public void onCheckSuccess(String type) {
        dismissLoadingDialog();
        Intent intent = new Intent(MyBankCardActivity.this, BindBankCardOldActivity.class);
        intent.putExtra("type", 1);
//        intent.putExtra("name", et_qr_name.getText().toString());
//        intent.putExtra("iccard", et_qr_id.getText().toString());
        startActivity(intent);
    }

    @Override
    public void onEvent(MessageEvent event) {
        if (MessageEvent.CODE_BIND_BANK_CARD_SUCCESS == event.getCode()) {
            mMyBankCardPresenter.gainMyBankCard();
        }
    }

    @Override
    public void onCheckFailed(GetMyBankResponse getMyBankResponse) {
        dismissLoadingDialog();
        Toast.makeText(mContext, getMyBankResponse.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRechargeSuccess() {

    }

    @Override
    public void onRechargeFailed(GetCodeResponse getCodeResponse) {
        Toast.makeText(mContext, getCodeResponse.getMessage(), Toast.LENGTH_SHORT).show();
    }


    class poponDismissListener implements PopupWindow.OnDismissListener {

        @Override
        public void onDismiss() {
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
        }
    }

    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }

    @Override
    public void onGainMyBankCard(final List<BankCard> bankCards) {
        tv_qr_change_bank_card.setVisibility(View.VISIBLE);
        mAdapter.setNewData(bankCards);
    }

    @Override
    public void onGainMyBankCardFail(GetMyBankResponse getMyBankResponse) {
        tv_qr_change_bank_card.setVisibility(View.GONE);
    }
}
